package _05_二叉树;

import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
 */
public class _559_N叉树的最大深度 {

    // 递归
    public int maxDepth1(Node root) {
        if (root == null) return 0;

        int max = 0;
        List<Node> nodes = root.children;
        if (nodes != null && !nodes.isEmpty()) {
            for (Node node : nodes) {
                max = Math.max(max, maxDepth(node));
            }
        }
        return max + 1;
    }

    // 迭代
    public int maxDepth(Node root) {
        if (root == null) return 0;
        LinkedList<Node> queue = new LinkedList<>();
        queue.offer(root);
        int height = 0;
        int levelSize = 1;
        while (!queue.isEmpty()) {
            root = queue.poll();

            List<Node> nodes = root.children;
            if (nodes != null && !nodes.isEmpty()) {
                for (Node node : nodes) {
                    queue.offer(node);
                }
            }

            if (--levelSize == 0) {
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }

}
